package com.app.brilliant.game.service.impl;

import com.app.brilliant.common.service.impl.BaseServiceImpl;
import com.app.brilliant.game.bean.BriClassHour;
import com.app.brilliant.game.bean.BriUserExpInfo;
import com.app.brilliant.game.bean.BriUserHourComplete;
import com.app.brilliant.game.response.BriCourseCompleteInfo;
import com.app.brilliant.game.service.BriClassHourService;
import com.app.brilliant.game.service.BriUserExpInfoService;
import com.app.brilliant.game.service.BriUserHourCompleteService;
import com.app.brilliant.game.mapper.BriUserHourCompleteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author GF
* @description 针对表【bri_user_hour_complete】的数据库操作Service实现
* @createDate 2025-09-02 17:16:39
*/
@Service
public class BriUserHourCompleteServiceImpl extends BaseServiceImpl<BriUserHourCompleteMapper, BriUserHourComplete>
implements BriUserHourCompleteService{
    @Autowired
    private BriClassHourService briClassHourService;
    @Autowired
    private BriUserExpInfoService briUserExpInfoService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean completeClassHour(Integer classHourId, Integer userId) {
        // 1. 检查课时是否存在
        BriClassHour classHour = briClassHourService.getById(classHourId);
        if (classHour == null) {
            return false;
        }
        // 2. 检查用户课时完成记录是否存在
        BriUserHourComplete userClassHour = baseMapper.getUserClassHour(userId, classHourId);
        // 3. 如果不存在，则创建新记录
        if (userClassHour == null) {
            userClassHour = new BriUserHourComplete();
            userClassHour.setUid(userId);
            userClassHour.setHId(classHourId);
            userClassHour.setComplete(1); // 设置为已完成
            save(userClassHour);
        } else if (userClassHour.getComplete() == 1) {
            // 如果已经完成，则直接返回成功
            return true;
        } else {
            // 更新为已完成
            userClassHour.setComplete(1);
            updateById(userClassHour);
        }
        // 4. 更新用户经验值
        BriUserExpInfo userExpInfo = briUserExpInfoService.getById(userId);
        if (userExpInfo == null) {
            // 如果用户经验记录不存在，则创建新记录
            userExpInfo = new BriUserExpInfo();
            userExpInfo.setUid(userId);
            userExpInfo.setExp(classHour.getExp()); // 设置初始经验值为当前课时经验
            userExpInfo.setCreateTime(new Date()); // 设置初始等级为1
            briUserExpInfoService.save(userExpInfo);
        } else {
            // 更新用户经验值
            Integer currentExp = userExpInfo.getExp() == null ? 0 : userExpInfo.getExp();
            userExpInfo.setExp(currentExp + classHour.getExp());
            // 这里可以添加等级计算逻辑
            briUserExpInfoService.updateById(userExpInfo);
        }
        return true;
    }

    @Override
    public List<BriCourseCompleteInfo> getCourseCompleteInfo(Integer courseId, Integer userId) {
        return baseMapper.getCourseCompleteInfo(courseId, userId);
    }

    @Override
    public boolean hasCompletionRecordOnDate(LocalDate date, Integer userId) {
        List<BriUserHourComplete> records = baseMapper.selectByUserIdAndDate(userId, date);
        return !records.isEmpty();
    }

    @Override
    public BriUserHourComplete getByUserIdAndClassHourId(Integer userId, Integer classHourId) {
        return baseMapper.getUserClassHour(userId, classHourId);
    }
}
